package com.huang.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.huang.servicebase.execptionhandler.huangException;
import com.huang.vod.service.VodService;
import com.huang.vod.utils.ContsantVodUtils;
import com.huang.vod.utils.InitVodClient;
import com.hunag.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("eduvod/video")
public class VodController {
    @Autowired
    private VodService service;

    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){
       String id= service.uploadVideo(file);
        return R.ok().data("videoId",id);
    }

    //删除视频
    @DeleteMapping("removeAliyunVideo/{id}")
    public R removeAliyunVideo(@PathVariable String id){

        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ContsantVodUtils.ACCESS_KEY_ID, ContsantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        }catch(Exception e) {
            e.printStackTrace();
            throw new huangException(20001,"删除视频失败");
        }

    }

    @DeleteMapping("deleteVedioList")
    public R deleteVedioList(@RequestParam("videoList") List videoId){
            service.deleteVedioList(videoId);

        return R.ok();
    }
    //根据视频id获取凭证
    @GetMapping("getAuth/{id}")
    public R getAuth(@PathVariable String id){
    try{
        DefaultAcsClient client = InitVodClient.initVodClient(ContsantVodUtils.ACCESS_KEY_ID, ContsantVodUtils.ACCESS_KEY_SECRET);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(id);
       GetVideoPlayAuthResponse response=client.getAcsResponse(request);
        String playAuth = response.getPlayAuth();
        return R.ok().data("auth",playAuth);
    }catch (Exception e){
        throw new huangException(20001,"获取凭证失败");
    }


    }




}
